package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.Utils.ImageUploadUtil;
import com.example.Utils.LoggerUtils;
import com.example.model.Menu;
import com.example.dao.MenuMapper;
import com.example.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hello
 * @since 2024-07-13
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    private static final Logger logger = LoggerUtils.getLogger(MenuServiceImpl.class);
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    ImageUploadUtil imageUploadUtil;
    @Override
    public List<Menu> getMenuByRestaurantId(Integer restaurantId) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.eq("restaurant_id",restaurantId);
        List<Menu> menus = menuMapper.selectList(menuQueryWrapper);
        List<Menu> generatePresignedUrl = null;
        try {
            generatePresignedUrl = imageUploadUtil.generatePresignedUrl(menus);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return generatePresignedUrl;
    }

    @Override
    public Boolean addDish(Menu menu) {
        if(isAdded(menu) != null) return false;
        String dishImage = menu.getDishImage();
        String image = imageUploadUtil.uploadBase64Image(dishImage, menu.getDishName(),menu.getRestaurantId());
        menu.setDishImage(image);
        return menuMapper.insert(menu) == 1 ? true : false;
    }

    //这个接口既能看是否有某个菜，也能返回这个菜
    @Override
    public Menu isAdded(Menu menu) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        Integer restaurantId = menu.getRestaurantId();
        String dishName = menu.getDishName();
        menuQueryWrapper.eq("restaurant_id",restaurantId).eq("dish_name",dishName);
        Menu one = menuMapper.selectOne(menuQueryWrapper);
        return one;
    }

    @Override
    public Menu getDish(Menu menu) {
        Menu dish = isAdded(menu);
        List<Menu> generatePresignedUrl = null;
        ArrayList<Menu> menus = new ArrayList<>();
        menus.add(dish);
        try {
            generatePresignedUrl = imageUploadUtil.generatePresignedUrl(menus);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return generatePresignedUrl.get(0);
    }

    @Override
    public Boolean deleteDish(Menu menu) {
        Menu added = isAdded(menu);
        if(added == null) return false;
        String dishImage = added.getDishImage();
        try {
            imageUploadUtil.deleteImage(dishImage);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        Integer restaurantId = menu.getRestaurantId();
        String dishName = menu.getDishName();
        menuQueryWrapper.eq("restaurant_id",restaurantId).eq("dish_name",dishName);
        int delete = menuMapper.delete(menuQueryWrapper);
        return delete == 1 ? true : false;
    }

    public String getMenuIdByRestaurantIdAndMenuName(String restaurantId,String menuName){
        return menuMapper.getIdByRestaurantIdAndDishName(restaurantId,menuName);
    }
}
